package com.test.turntable;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.RotateAnimation;
import android.widget.Button;

import com.test.turntable.widgets.TrunTableView;

public class MainActivity extends Activity implements OnClickListener, AnimationListener {

	int mTurnCount = 5;
	boolean mIsTurnning = false;
	TrunTableView turnTable;
	Button btnStart;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btnStart = (Button) findViewById(R.id.btnStart);
		btnStart.setOnClickListener(this);
		turnTable = (TrunTableView) findViewById(R.id.turnTable);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnStart:
			if (mIsTurnning) {
				turnTable.clearAnimation();
			} else {
				RotateAnimation rotateAnimation = new RotateAnimation(0, mTurnCount * 360 - 1 + getGoft() * 45, Animation.RELATIVE_TO_SELF, 0.5f,
						Animation.RELATIVE_TO_SELF, 0.5f);
				rotateAnimation.setDuration((mTurnCount - 1) * 1000);
				rotateAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
				rotateAnimation.setFillAfter(true);
				rotateAnimation.setAnimationListener(this);
				turnTable.startAnimation(rotateAnimation);
			}
			break;

		default:
			break;
		}
	}

	private int getGoft() {
		Random random = new Random(System.currentTimeMillis());
		return random.nextInt() % 8;

	}

	@Override
	public void onAnimationStart(Animation animation) {
		Log.d("test", "start");
		mIsTurnning = true;
		btnStart.setText("Cancel");
	}

	@Override
	public void onAnimationEnd(Animation animation) {
		Log.d("test", "end");
		mIsTurnning = false;
		btnStart.setText("Start");
	}

	@Override
	public void onAnimationRepeat(Animation animation) {
	}

}
